//
//  LKRefundMoneyViewController.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/7/14.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKRefundMoneyViewController.h"
#import <IQKeyboardManager.h>
#import "LKLockScanViewController.h"
#import "LKCustomKeyBoardView.h"
#import "LKBillDetailViewController.h"
@interface LKRefundMoneyViewController ()<LKCustomKeyBoardViewDelegate>

@property(nonatomic,strong)UITextField *moneyTf;
@property(nonatomic,strong)UITextField *noteTf;

@end

@implementation LKRefundMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"退款";
    UIView *moneyView = [self createHeaderViewWithTitle:@"请输入退款金额"];
    [self.view addSubview:moneyView];
    [moneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT + 30);
        make.height.mas_equalTo(170);
    }];
    UIView *noteView = [self createNoteViewWithTitle:@"可输入财务系统订单号或其他描述"];
    [self.view addSubview:noteView];
    [noteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.top.equalTo(moneyView.mas_bottom).offset(20);
        make.height.mas_equalTo(52);
    }];
    // Do any additional setup after loading the view.
}

/// 创建视图
/// @param placeHolder 输入框占位符
-(UIView *)createHeaderViewWithTitle:(NSString *)placeHolder{
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    LKBaseLabel *headerLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"实收金额" textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"PingFangSC-Medium" size:16] textAlignment:NSTextAlignmentCenter];
    [bgView addSubview:headerLabel];
    [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgView).offset(0);
        make.top.equalTo(bgView).offset(10);
    }];
    LKBaseLabel *moneyLabel = [LKBaseLabel labelWithFrame:CGRectZero text:[NSString stringWithFormat:@"￥%@",_price] textColor:[UIColor redColor] font:[UIFont fontWithName:@"PingFangSC-Regular" size:16] textAlignment:NSTextAlignmentCenter];
    [bgView addSubview:moneyLabel];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgView).offset(0);
        make.top.equalTo(headerLabel.mas_bottom).offset(10);
    }];
    LKBaseLabel *yuanLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"退款金额" textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"PingFangSC-Medium" size:16] textAlignment:NSTextAlignmentLeft];
    [bgView addSubview:yuanLabel];
    [yuanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(20);
        make.top.equalTo(moneyLabel.mas_bottom).offset(30);
    }];
    self.moneyTf = [[UITextField alloc] init];
//    self.moneyTf.xmKeyBoardType = XMkeyBoardType_NumberStyleOne;
    self.moneyTf.placeholder = placeHolder;
    self.moneyTf.textAlignment = NSTextAlignmentRight;
    self.moneyTf.text = self.price;
    self.moneyTf.keyboardType = UIKeyboardTypeDecimalPad;
    [self.moneyTf becomeFirstResponder];
    LKCustomKeyBoardView *boradView = [[LKCustomKeyBoardView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH) AndreturnTitle:@"退款"];
    boradView.delegate = self;
    self.moneyTf.inputView = boradView;
    [bgView addSubview:self.moneyTf];
    [self.moneyTf mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(yuanLabel.mas_right).offset(20);
       make.right.equalTo(bgView.mas_right).offset(-10);
       make.centerY.equalTo(yuanLabel.mas_centerY).offset(5);
    }];
    LKBaseLabel *bottomLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"暂仅支付全额退款,资金原路返回到顾客账户" textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"PingFangSC-Regular" size:12] textAlignment:NSTextAlignmentRight];
    [bgView addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView.mas_right).offset(-20);
        make.top.equalTo(self.moneyTf.mas_bottom).offset(15);
    }];

    return bgView;
}
/// 创建备注view
/// @param note 备注描述
-(UIView *)createNoteViewWithTitle:(NSString *)note{
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    LKBaseLabel *headerLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"备注" textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"PingFangSC-Medium" size:16] textAlignment:NSTextAlignmentLeft];
    [bgView addSubview:headerLabel];
    [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(20);
        make.centerY.equalTo(bgView);
    }];
    self.noteTf = [[UITextField alloc] init];
    self.noteTf.placeholder = note;
    self.noteTf.textAlignment = NSTextAlignmentLeft;
    self.moneyTf.keyboardType = UIKeyboardTypeDecimalPad;
    [bgView addSubview:self.noteTf];
    [self.noteTf mas_makeConstraints:^(MASConstraintMaker *make) {
       make.right.equalTo(bgView.mas_right).offset(-20);
       make.left.equalTo(headerLabel.mas_right).offset(20);

       make.centerY.equalTo(bgView);
    }];
    return bgView;
}
#pragma mark -LKCustomKeyBoardViewDelegate

-(UIButton *)lkClickConten:(UIButton *)contenBtn{
    
    return contenBtn;
}
-(UIButton *)lkClickDelete:(UIButton *)deleteBtn{
    
    return deleteBtn;
}
-(UIButton *)lkClickClear:(UIButton *)clearBtn{
    
    return clearBtn;
}
-(UIButton *)lkClickdetermine:(UIButton *)determineBtn{
    WS(weakSelf)
    NSLog(@"点击了%@",determineBtn.currentTitle);
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"退款提示" message:[NSString stringWithFormat:@"￥%@\n请确认退款，资金将原路返回到顾客的账户",_price] preferredStyle:UIAlertControllerStyleAlert];
     [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
      [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{@"reason":weakSelf.noteTf.text,@"price_back":self.moneyTf.text,@"style":@"2",@"remark":weakSelf.remark}];
          NSString *sign = [NSString encoingWithDic:param];
          [param setObject:sign forKey:@"sign"];
          [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyPayBackUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
              
          } success:^(BOOL isSuccess, id  _Nullable responseObject) {

              if ([responseObject[@"code"] isEqualToString:@"success"]) {
                  [SVProgressHUD showInfoWithStatus:@"退款成功"];
                  NSDictionary * dataDic = responseObject[@"back_info"];
                  LKBillListModel *model = [LKBillListModel modelWithDictionary:dataDic];
                  LKBillDetailViewController *vc = [[LKBillDetailViewController alloc] init];
                  vc.model = model;
                  [weakSelf.navigationController pushViewController:vc animated:YES];
              }else{
                  [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
              }

          } failure:^(NSString * _Nullable errorMessage) {
              
          }];
      }]];
    dispatch_async(dispatch_get_main_queue(), ^{
          [self presentViewController:alert animated:YES completion:nil];
    });

    return determineBtn;
}
//  键盘弹出触发该方法
- (void)keyboardDidShow:(NSNotification *)aNotification {
    NSLog(@"键盘弹出");
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    NSLog(@"键盘高度；%d",height);
}
//  键盘隐藏触发该方法
- (void)keyboardDidHide:(NSNotification *)aNotification {
    NSLog(@"键盘隐藏");
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
